<template>
  <div>
    <div class="operate-bar">
      <div class="btn-group">
        <el-button type="success" @click="save(10)">提交</el-button>
        <!-- <el-button type="primary" @click="save(0)">暂存</el-button> -->
        <el-button type="info" @click="goBack">返回</el-button>
      </div>
    </div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="150px"
      :inline="false"
    >
      <el-form-item label="整改单编号" prop="mend_code">
        <el-input
          style="width: 500px"
          v-model="form.mend_code"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item label="整改项目" prop="object_id">
        <el-select
          style="width: 500px"
          v-model="form.object_id"
          value-key=""
          placeholder="输入关键字筛选"
          filterable
          clearable
        >
          <el-option
            v-for="item in projects"
            :key="item.value"
            :label="item.project_name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题来源" prop="problem_source">
        <el-select v-model="form.problem_source" value-key="" placeholder="">
          <el-option :key="1" label="巡查检查" :value="1"></el-option>
          <el-option :key="2" label="单位内审" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" prop="">
        <el-checkbox
          :true-label="1"
          :false-label="0"
          v-model="form.problem_type_safety"
          >工程安全</el-checkbox
        >
        <el-checkbox
          :true-label="1"
          :false-label="0"
          v-model="form.problem_type_quality"
          >工程质量</el-checkbox
        >
        <el-checkbox
          :true-label="1"
          :false-label="0"
          v-model="form.problem_type_build"
          >文明施工</el-checkbox
        >
        <el-checkbox
          :true-label="1"
          :false-label="0"
          v-model="form.problem_type_duty"
          >人员到岗</el-checkbox
        >
      </el-form-item>
      <el-form-item label="疑似事项">
        <div style="display: flex">
          <el-form-item label-width="100px" label="非法转包" prop="recontract">
            <el-radio v-model="form.recontract" label="1">是</el-radio>
            <el-radio v-model="form.recontract" label="0">否</el-radio>
          </el-form-item>
          <el-form-item
            style="margin-left: 20px"
            label-width="100px"
            label="违法分包"
            prop="subcontract"
          >
            <el-radio v-model="form.subcontract" label="1">是</el-radio>
            <el-radio v-model="form.subcontract" label="0">否</el-radio>
          </el-form-item>
        </div>
      </el-form-item>

      <el-form-item label="问题描述" prop="problem_desc">
        <el-input
          type="textarea"
          v-model="form.problem_desc"
          :autosize="{
            minRows: 3,
            maxRows: 6,
          }"
          placeholder=""
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="相同问题重复次数" prop="">
        <el-input-number
          style="width: 150px"
          v-model="form.repeat_count"
          label=""
          :min="0"
          :max="99"
          :step="1"
          :controls="true"
          controls-position="right"
        >
        </el-input-number>
      </el-form-item>
      <el-form-item label="整改要求" prop="mend_require">
        <el-input
          type="textarea"
          v-model="form.mend_require"
          :autosize="{
            minRows: 3,
            maxRows: 6,
          }"
          placeholder=""
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="检查单位" prop="inspect_unit">
        <el-input
          style="width: 500px"
          v-model="form.inspect_unit"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item label="检查人员" prop="inspector">
        <el-input
          style="width: 500px"
          v-model="form.inspector"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item label="反馈日期要求" prop="feedback_date_require">
        <el-date-picker
          v-model="form.feedback_date_require"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期时间"
          :picker-options="{
            disabledDate: (c) =>
              c.getTime() < currentDate.getTime() - 1000 * 60 * 60 * 24,
          }"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成日期要求" prop="finish_date_require">
        <el-date-picker
          v-model="form.finish_date_require"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期时间"
          :picker-options="{
            disabledDate: (c) =>
              c.getTime() < currentDate.getTime() - 1000 * 60 * 60 * 24,
          }"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="登记日期" prop="">
        <el-date-picker
          v-model="currentDate"
          disabled
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProjectList } from "@/api/Project/Index";
import { addCheckAndRectify } from "@/api/CheckAndRectify/Index";
import { required } from "@/utils/eValidate";
export default {
  data() {
    return {
      form: {
        // 整改单编号
        mend_code: "",
        // 整改对象
        object_name: "",
        // 整改单对象ID
        object_id: "",
        // 整改对象类型(1、项目)
        object_type: 1,
        // 问题来源
        problem_source: "",
        // 问题分类
        // problem_type: "",
        // 非法转包
        recontract: "",
        // 违法分包
        subcontract: "",
        // 问题描述
        problem_desc: "",
        // 相同问题重复次数
        repeat_count: 0,
        // 检查单位
        inspect_unit: "",
        // 检查人员
        inspector: "",
        // 整改要求
        mend_require: "",
        // 反馈日期要求
        feedback_date_require: "",
        // 完成日期要求
        finish_date_require: "",

        // 状态 0暂存 10待反馈 20待审核 30已退回 100处理完毕
        status: 0,
        // 状态文本
        status_text: "",

        // 问题类型
        problem_type_safety: 0,
        problem_type_quality: 0,
        problem_type_build: 0,
        problem_type_duty: 0,
      },
      currentDate: new Date(),
      rules: {
        mend_code: [required],
        object_name: [required],
        problem_source: [required],
        problem_desc: [required],
        inspect_unit: [required],
        inspector: [required],
        mend_require: [required],
        feedback_date_require: [required],
        finish_date_require: [required],
        recontract: [required],
        subcontract: [required],
      },
      projects: [],
    };
  },
  methods: {
    initData() {},
    initProject() {
      getProjectList({
        pageNum: 1,
        pageSize: 1000,
      }).then((res) => {
        this.projects = res.data;
      });
    },
    goBack() {
      this.$router.push("/checkAndRectifyRegistration");
    },
    save(status) {
      this.$refs.form.validate().then((valid) => {
        this.form.status = status;
        switch (status) {
          case 0:
            this.form.status_text = "暂存";
            break;
          case 10:
            this.form.status_text = "待反馈";
            break;

          default:
            break;
        }

        this.form.object_name = this.projects.find(
          (i) => i.id == this.form.object_id
        ).project_name;

        addCheckAndRectify(this.form).then((res) => {
          if (res.code == 0) {
            this.$message.success("保存成功");
            this.goBack();
          }
        });
      });
    },
  },
  created() {
    this.initData();
    this.initProject();
  },
};
</script>

<style lang="scss" scoped></style>
